﻿using Mapster;
using Masa.BuildingBlocks.Data.UoW;
using Masa.Contrib.Ddd.Domain.Repository.EFCore;
using Masa.CqrsProject.Contexts;
using Masa.CqrsProject.Models;
using Microsoft.EntityFrameworkCore;
using System.Runtime.InteropServices;

namespace Masa.CqrsProject.Repositorys
{
    /// <summary>
    /// 仓储实现
    /// </summary>
    public class OrderRepository : Repository<MyDbContext, OrderInfo, int>, IOrderRepository
    {
        private readonly MyDbContext _myDbContext;
        private readonly CqrsWriteDbContext _cqrsWriteDbContext;
        private readonly IUnitOfWork _unitOfWork;

        /// <summary>
        /// 
        /// </summary>
        public OrderRepository(MyDbContext context, IUnitOfWork unitOfWork, CqrsWriteDbContext cqrsWriteDbContext   ) : base(context, unitOfWork)
        {
            _myDbContext = context;
            _unitOfWork = unitOfWork;
            _cqrsWriteDbContext = cqrsWriteDbContext;
        }


        /// <summary>
        /// 创建订单
        /// </summary>
        /// <param name="order"></param>
        /// <exception cref="NotImplementedException"></exception>
        public async Task CreateAsync(OrderInfo order)
        {
            try
            {
                _myDbContext.OrderInfo.Add(order);
                _myDbContext.OrderItems.AddRange(order.OrderItems);
                await _myDbContext.SaveChangesAsync();
            }
            catch (Exception e)
            {
                throw;
            }
        }

        /// <summary>
        /// 更新订单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public async Task UpdateAsync(OrderInfo model)
        {
            try
            {
                var orderInfo = await _cqrsWriteDbContext.OrderInfo.Where(it => it.Id == model.Id).FirstOrDefaultAsync();
                if (orderInfo == null) {

                    throw new Exception("订单不存在...");
                }

                orderInfo.OrderSn = model.OrderSn;
                 _cqrsWriteDbContext.Update(orderInfo);

                var res = await _cqrsWriteDbContext.SaveChangesAsync();
                if (res > 0)
                {

                }

            }
            catch (Exception e)
            {
                throw;
            }
        }

        /// <summary>
        /// 查询订单
        /// </summary>
        /// <param name="orderWhere"></param>
        /// <returns></returns>
        public async Task<OrderInfo> GetOrderAsync(string orderWhere)
        {
            //使用导航查询将关联的OrderItems也查询出来
            Console.WriteLine("查询订单...");
            var order = _myDbContext.OrderInfo
                .Include(o => o.OrderItems).ToList()
                .Where((a, b) => a.OrderSn == orderWhere || a.Id.ToString() == orderWhere)
                .FirstOrDefault();
            return order;
        }

        /// <summary>
        /// 查询订单列表
        /// </summary>
        /// <returns></returns>
        public async Task<List<OrderInfo>> GetOrderListAsync(string whereStr)
        {
            Console.WriteLine("查询订单列表...");
            var orderList = _myDbContext.OrderInfo.Include(o => o.OrderItems).ToList();
            if (!string.IsNullOrEmpty(whereStr))
            {
                orderList = orderList.Where((a, b) => a.OrderSn == whereStr).ToList();
            }
            return orderList;
        }


        /// <summary>
        /// 删除订单
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task DeleteAsync(int id)
        {
            Console.WriteLine("删除订单...");
            var order = _myDbContext.OrderInfo.Include(o => o.OrderItems).ToList().Where((a, b) => a.Id == id).FirstOrDefault();
            if (order != null)
            {
                _myDbContext.OrderInfo.Remove(order);
                _myDbContext.OrderItems.RemoveRange(order.OrderItems);
                await _myDbContext.SaveChangesAsync();
            }
        }

    }
}
